/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Model.IntersectionInfo;
import View.Display;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.zip.DataFormatException;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class Simulator {

    /**
     * @param args the command line arguments
     */
    private static Display mainDisplay;
    private static StartButtonListner startButtonHandler;
    private static EndButtonListner endButtonHandler;
    private static IntersectionInfo oIntersectionInfo;
    private static Traffic oTraffic;
    private static boolean isTafficOnGoing;

    public static void main(String[] args) {

        // create intersection information class object (Model)
        oIntersectionInfo = new IntersectionInfo();

        // create traffic flow class object (Controller)
        oTraffic = new Traffic(oIntersectionInfo);

        // create display class object (View)
        mainDisplay = new Display(oIntersectionInfo, oTraffic);

        startButtonHandler = new StartButtonListner();
        endButtonHandler = new EndButtonListner();
        mainDisplay.addListenerSetInfoButton(new SetInfoButtonListner());
        mainDisplay.addListenerStartButton(new StartButtonListner());
        mainDisplay.addListenerEndButton(new EndButtonListner());

        mainDisplay.setVisible(true);

    }

    public static class SetInfoButtonListner implements ActionListener  {

        public void actionPerformed(ActionEvent e) {

            try {
                int nLaneHoriz = mainDisplay.getLanesHoriz();
                int nLaneVert = mainDisplay.getLanesVert();
                float fProbabilityHoriz = mainDisplay.getProbabilityHoriz();
                float fProbabilityVert = mainDisplay.getProbabilityVert();

                oIntersectionInfo.setLaneHoriz(nLaneHoriz);
                oIntersectionInfo.setLaneVert(nLaneVert);
                oIntersectionInfo.setProbabilityHoriz(fProbabilityHoriz);
                oIntersectionInfo.setProbabilityVert(fProbabilityVert);

                // recreate traffic flow class object (Controller)
                oTraffic = new Traffic(oIntersectionInfo);

                // replace intersection info object for display class
                mainDisplay.setIntersectionInfo(oIntersectionInfo);
                // replace traffic object for display class
                mainDisplay.setTraffic(oTraffic);

                mainDisplay.repaint();

            }
            catch (DataFormatException exp){

                JOptionPane.showMessageDialog(null, exp, "Traffic Simulator Error", JOptionPane.ERROR_MESSAGE);
            }
            catch (Exception exp){

                JOptionPane.showMessageDialog(null, exp, "Traffic Simulator Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public static class StartButtonListner implements ActionListener  {

        public void actionPerformed(ActionEvent e) {

            isTafficOnGoing = true;

            Thread oThreadTraffic = new Thread(){

                @Override
                public void run() {

                    int nRoundMax;

                    while(isTafficOnGoing){

                        // execute single traffic flow
                        nRoundMax = 1;
                        oTraffic.doTrafficFlow(nRoundMax);
                        mainDisplay.repaint();
                        try {
                            Thread.sleep(500);
                        }
                        catch (Exception e){

                        }
                    }
                }
            };

            oThreadTraffic.start();
        }
    }

    public static class EndButtonListner implements ActionListener  {

        public void actionPerformed(ActionEvent e) {

            isTafficOnGoing = false;
        }
    }
}
